﻿using DevelopTool.Common;
using System;

namespace DevelopTool.Forms.LodingForm
{
    /// <summary>
    /// 加载提示帮助类
    /// </summary>
    public class LoadingHelper
    {
        private static LodingForm lodingForm;

        /// <summary>
        /// 显示加载窗口
        /// </summary>
        /// <param name="message">提示文本</param>
        /// <param name="work">执行的方法</param>
        /// <param name="topMost">是否现实中在顶层</param>
        public static void ShowLoading(string message, Action work, bool topMost = false)
        {
            if (string.IsNullOrEmpty(message))
            {
                message = "正在加载...";
            }
            if (message.LastIndexOf("...") < 0)
            {
                message += "...";
            }
            lodingForm = new LodingForm(message, work, topMost);

            

            lodingForm.ShowDialog();

            if (lodingForm.WorkException != null)
            {
                throw lodingForm.WorkException;
            }
            lodingForm.Dispose();
        }


        /// <summary>
        /// 设置加载窗口的文本
        /// </summary>
        /// <param name="msg"></param>
        public static void SetLoadingMsg(string msg)
        {
            if (lodingForm != null)
            {
                lodingForm.Message = msg;
            }
        }


    }
}
